/*
 * test_connection.cpp
 *
 *  Created on: Apr 21, 2014
 *      Author: killerg
 */

#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/dns.h>
#include <assert.h>

static void bev_read_cb(bufferevent *bev, void *ctx) {

}
static void bev_write_cb(bufferevent *bev, void *ctx) {

}
static void bev_event_cb(bufferevent *bev, short what, void *ctx) {

}

bool test_connection() {
	event_base* base = event_base_new();
	evdns_base* dns = evdns_base_new(base, 0);

	bufferevent* bev;

	// start
	bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
	bufferevent_setcb(bev, bev_read_cb, bev_write_cb, bev_event_cb, NULL);
	bufferevent_enable(bev, EV_WRITE|EV_READ);
	int ret = bufferevent_socket_connect_hostname(bev, dns, AF_INET, "localhost", 9876);
	assert(ret >= 0);

	// cancel
	bufferevent_free(bev);



	event_base_dispatch(base);

	evdns_base_free(dns, 1);
	event_base_free(base);

	return true;
}

/*

struct billsession_t {
	tran_c tran;
	double timer;
	void update(double delta) {
		if (!tran.is_connected() && !tran.is_connecting()) {
			timer -= delta;
			if (timer < 0) {
				timer = 5.0f;
				tran.connect("localhost", 9876);
			}
		}
	}
};

 */
